package com.cy.cy2025.util

import android.widget.Toast
import androidx.annotation.StringRes
import com.cy.cy2025.MyApplication

object ToastUtil {

    private var toast: Toast? = null

    /**
     * 显示短时间 Toast
     * @param message 要显示的消息
     */
    fun showShort(message: String) {
        cancelPreviousToast()
        toast = Toast.makeText(MyApplication.app, message, Toast.LENGTH_SHORT)
        toast?.show()
    }

    /**
     * 显示短时间 Toast
     * @param resId 字符串资源 ID
     */
    fun showShort(@StringRes resId: Int) {
        cancelPreviousToast()
        toast = Toast.makeText(MyApplication.app, resId, Toast.LENGTH_SHORT)
        toast?.show()
    }

    /**
     * 显示长时间 Toast
     * @param message 要显示的消息
     */
    fun showLong(message: String) {
        cancelPreviousToast()
        toast = Toast.makeText(MyApplication.app, message, Toast.LENGTH_LONG)
        toast?.show()
    }

    /**
     * 显示长时间 Toast
     * @param resId 字符串资源 ID
     */
    fun showLong(@StringRes resId: Int) {
        cancelPreviousToast()
        toast = Toast.makeText(MyApplication.app, resId, Toast.LENGTH_LONG)
        toast?.show()
    }

    /**
     * 取消上一个未显示完的 Toast
     */
    private fun cancelPreviousToast() {
        toast?.cancel()
    }
}